﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.ObjectBuilder;
using NetDon.OA.Business;
using NetDon.OA.Data.Linq;
using NetDon.OA.Interface.Common;

namespace NetDon.OA.Portal.Admin
{
    public partial class RouteInfoEdit : PageBase, IRouteInfoEditView
    {
        private RouteInfoEditPresenter _presenter;

        [CreateNew]
        public RouteInfoEditPresenter Presenter
        {
            set
            {
                _presenter = value;
                _presenter.View = this;
            }
        }

        #region ICurrencyEditView Members

        public Guid RouteId
        {
            get { return Parameters.GetGuid("OID"); }
        }

        public void PopulateRouteData(RouteData routeData)
        {
            formCurrency.DataSource = routeData;
            formCurrency.DataBind();
        }

        public void ExtractRouteData(RouteData routeData)
        {
            formCurrency.ExtractValues(routeData, false);
            routeData.AreaId = (CurrentApplicationContext as OApplicationContext).CompanyId;
        }

        public event EventHandler SaveRouteData;

        #endregion

        protected virtual void OnSaveVehicleModel(EventArgs e)
        {
            if (SaveRouteData != null)
            {
                SaveRouteData(this, e);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CancelButton = btnCancel;
                _presenter.OnViewInitialized();
            }
            _presenter.OnViewLoaded();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var metaData = new LinqMetaData();
            var count = (from entity in metaData.Route
                         where entity.RouteName == txtRouteName.Text.Trim()
                         select entity.Id).Count();
            bool duplciateNameExists = RouteId == Guid.Empty ? count > 0 : count > 1;
            if (duplciateNameExists)
            {
                Alert("此路线已存在，请更换名称.");
                return;
            }
            OnSaveVehicleModel(EventArgs.Empty);
        }


    }
}